import roleApi from '@/api/roleApi'
import type { Params, RoleItem } from '@/types/roleType'
import { useAntdTable } from 'ahooks'
import { Button, Form, Input, message, Space, Table, type TableColumnsType } from 'antd'
import CreateRole from './CreateRole'
import { useRef } from 'react'
import type { IAction } from '@/types/modal'
import { formatDate } from '@/utils'
import { modal } from '@/utils/AntdGlobal'
import SetPermission from './SetPermission'

const RoleList = () => {
  const [form] = Form.useForm()
  const roleRef = useRef<{ open: (type: IAction, data?: RoleItem) => void }>(undefined)
  const permissionRef = useRef<{ open: (type: IAction, data?: RoleItem) => void }>(undefined)
  const getTableData = ({ current, pageSize }: { current: number; pageSize: number }, formatData: Params) => {
    return roleApi.getRoleList({ ...formatData, pageSize, pageNum: current }).then(res => {
      return {
        total: res.page.total,
        list: res.list
      }
    })
  }
  const handleCreate = () => {
    roleRef.current?.open('create')
  }
  const handleEdit = (data: RoleItem) => {
    roleRef.current?.open('edit', data)
  }
  const handleDel = (_id: string) => {
    modal.confirm({
      title: '删除确认',
      content: '确认删除该角色吗',
      onOk: () => {
        handleSubmitDel(_id)
      }
    })
  }
  const handleSubmitDel = async (_id: string) => {
    await roleApi.delRoleById({ _id })
    message.success('删除角色成功')
    search.submit()
  }
  const handleSet = (data: RoleItem) => {
    permissionRef.current?.open('edit', data)
  }
  const columns: TableColumnsType<RoleItem> = [
    {
      title: '角色名称',
      key: 'roleName',
      dataIndex: 'roleName'
    },
    {
      title: '备注',
      key: 'remark',
      dataIndex: 'remark'
    },
    {
      title: '更新时间',
      key: 'updateTime',
      dataIndex: 'updateTime',
      render: (value: Date) => {
        return formatDate(value)
      }
    },
    {
      title: '创建时间',
      key: 'createTime',
      dataIndex: 'createTime',
      render: (value: Date) => {
        return formatDate(value)
      }
    },
    {
      title: '操作',
      render: (record: RoleItem) => {
        return (
          <Space>
            <Button type='text' onClick={() => handleEdit(record)}>
              编辑
            </Button>
            <Button type='text' onClick={() => handleSet(record)}>
              设置权限
            </Button>
            <Button type='text' danger onClick={() => handleDel(record._id)}>
              删除
            </Button>
          </Space>
        )
      }
    }
  ]
  const { tableProps, search } = useAntdTable(getTableData, { form })
  return (
    <div className='role-list'>
      <Form layout='inline' form={form} className='search-form'>
        <Form.Item label='角色名称' name='roleName'>
          <Input></Input>
        </Form.Item>
        <Form.Item>
          <Space>
            <Button type='primary' onClick={search.submit}>
              搜索
            </Button>
            <Button onClick={search.reset}>重置</Button>
          </Space>
        </Form.Item>
      </Form>
      <div className='base-table'>
        <div className='header-wrapper'>
          <div className='title'>角色列表</div>
          <div className='action'>
            <Button type='primary' onClick={handleCreate}>
              新增
            </Button>
          </div>
        </div>
        <Table rowKey='_id' columns={columns} {...tableProps}></Table>
      </div>
      <CreateRole mRef={roleRef} update={search.submit}></CreateRole>
      <SetPermission mRef={permissionRef} update={search.submit}></SetPermission>
    </div>
  )
}

export default RoleList
